package com.maidada.codesharebackend.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.maidada.codesharebackend.common.BaseRequest;
import com.maidada.codesharebackend.common.CommonResult;
import com.maidada.codesharebackend.common.PageResult;
import com.maidada.codesharebackend.constant.UserRoleConstant;
import com.maidada.codesharebackend.model.dto.code.CodeAddRequest;
import com.maidada.codesharebackend.model.dto.code.CodeQueryRequest;
import com.maidada.codesharebackend.model.dto.code.CodeUpdateRequest;
import com.maidada.codesharebackend.model.vo.CodeVO;
import com.maidada.codesharebackend.service.CodeService;
import com.maidada.codesharebackend.service.CodeViewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 代码表 Controller
 *
 * @author wulinxuan
 */
@Slf4j
@RestController
@RequestMapping("/code")
public class CodeController {

    @Resource
    private CodeService codeService;

    @Resource
    private CodeViewService codeViewService;

    /**
     * 新增代码
     *
     * @param codeAddRequest 新增请求
     * @return 代码id
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/add")
    public CommonResult<Long> addCode(@Valid @RequestBody CodeAddRequest codeAddRequest) {
        Long codeId = codeService.addCode(codeAddRequest);
        return CommonResult.success(codeId);
    }

    /**
     * 删除代码
     *
     * @param baseRequest 请求参数
     * @return 是否成功
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/delete")
    public CommonResult<Boolean> deleteCode(@Valid @RequestBody BaseRequest baseRequest) {
        Boolean result = codeService.deleteCode(baseRequest.getId());
        return CommonResult.success(result);
    }

    /**
     * 修改代码
     *
     * @param codeUpdateRequest 修改请求
     * @return 是否成功
     */
    @SaCheckRole(UserRoleConstant.ADMIN)
    @PostMapping("/update")
    public CommonResult<Boolean> updateCode(@Valid @RequestBody CodeUpdateRequest codeUpdateRequest) {
        Boolean result = codeService.updateCode(codeUpdateRequest);
        return CommonResult.success(result);
    }

    /**
     * 分页查询代码
     *
     * @param codeQueryRequest 查询请求
     * @return 分页结果
     */
    @GetMapping("/page")
    public CommonResult<PageResult<CodeVO>> pageCode(@Valid CodeQueryRequest codeQueryRequest) {
        PageResult<CodeVO> pageResult = codeService.pageCode(codeQueryRequest);
        return CommonResult.success(pageResult);
    }

    /**
     * 获取代码详情
     *
     * @param id 代码id
     * @return 代码信息
     */
    @GetMapping("/get")
    public CommonResult<CodeVO> getCodeById(@RequestParam Long id) {
        CodeVO codeVO = codeService.getCodeById(id);
        return CommonResult.success(codeVO);
    }

    /**
     * 增加代码片段浏览次数
     * <p>
     * 该接口是异步的，会在后台线程中处理浏览次数的增加
     * 每个用户/IP每天只能增加一次浏览次数
     *
     * @param id 代码片段ID
     * @param request HTTP请求对象
     * @return 操作结果
     */
    @PostMapping("/view")
    public CommonResult<Boolean> incrementViewCount(@RequestParam Long id, HttpServletRequest request) {
        // 获取当前用户id
        Object loginId = StpUtil.getLoginIdDefaultNull();
        Long userId = (loginId != null) ? (Long.parseLong(loginId.toString())) : null;

        // 调用异步Service
        codeViewService.incrementViewCount(id, userId, request);
        return CommonResult.success(true);
    }
}

